package com.example.mntz.util;

import com.example.mntz.dao.AccountMapper;
import com.example.mntz.model.Classs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.TriggerContext;

import java.util.Date;
import java.util.List;

//记账本定时任务
//@Configuration
//@EnableScheduling
public class ImepStatusSchedulingConfig implements SchedulingConfigurer {
    private ScheduledTaskRegistrar taskRegistrar;

    @Autowired
    AccountMapper mapper;


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        List<Classs> classses = mapper.selectdsbqs();
        for(Classs a :classses){
            Thread runnable = new Thread(()->{
                // 业务逻辑
            },"class_"+a.getClassId());

            taskRegistrar.addTriggerTask(
                    runnable,
                    triggerContext -> {

                        // 根据cron表达式，构建CronTrigger，计算下一次的执行时间
                        CronTrigger trigger = new CronTrigger(a.getClassZq());
                        return trigger.nextExecutionTime(triggerContext);
                    }
            );
        }
        this.taskRegistrar = taskRegistrar;
    }

}